/*
 *  This file is part of ZiggyStardust.
 *
 *  ZiggyStardust is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  ZiggyStardust is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.wisc.csvParser.plugins;

import com.wisc.csvParser.plugins.DataParserGleonServiceXML.events;
import javax.swing.JOptionPane;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 *
 * @author  lawinslow
 */
public class JPanelGleonXML extends javax.swing.JPanel {
    DataParserGleonServiceXML parser;
    
    /** Creates new form JPanelGleonXML
     * @param parser Underlying parser object
     */
    public JPanelGleonXML(DataParserGleonServiceXML parser) {
        initComponents();
        
        this.parser = parser;
        updatePanel();
        
        parser.addEventHandler(new DataParserGleonServiceXML.IEventHandler() {

            public void eventRaised(events e) {
                handleEvent(e);
            }
        });
    }
    
    private void handleEvent(DataParserGleonServiceXML.events e){
        if(e==DataParserGleonServiceXML.events.started){
            
        }else if(e==DataParserGleonServiceXML.events.stopped){
            
        }else if(e==DataParserGleonServiceXML.events.updated){
            updatePanel();
        }else if(e==DataParserGleonServiceXML.events.valsParsed){
            
        }
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        sourceList = new javax.swing.JList();
        addButton = new javax.swing.JButton();
        getCustomButton = new javax.swing.JButton();

        jScrollPane1.setViewportView(sourceList);

        addButton.setText("Add");
        addButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addButtonActionPerformed(evt);
            }
        });

        getCustomButton.setText("Get Custom");
        getCustomButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                getCustomButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 161, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(getCustomButton))
                    .addComponent(addButton))
                .addContainerGap(134, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 214, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(addButton))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(69, 69, 69)
                        .addComponent(getCustomButton)))
                .addContainerGap(46, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void addButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addButtonActionPerformed
        String name = JOptionPane.showInputDialog("Please enter source name.");
        if(name == null || name.compareTo("")==0){
            return;
        }
        
        String url = JOptionPane.showInputDialog("Please enter source URL.");
        if(url == null || url.compareTo("")==0){
            return;
        }
        
        parser.addSource(name, url);
    }//GEN-LAST:event_addButtonActionPerformed

    private void getCustomButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_getCustomButtonActionPerformed
        if(sourceList.getSelectedValue() != null){
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            String temp = null;
            try{
                //get starting retrieval interval date
                temp = JOptionPane.showInputDialog("Start Date (yyyy-MM-dd)");
                if(temp==null){//user hit cancel
                    return;
                }
                Date start = format.parse(temp);
                //get latest date to attempt retrieval
                temp = JOptionPane.showInputDialog("End Date (yyyy-MM-dd)");
                if(temp==null){//user hit cancel
                    return;
                }
                Date end = format.parse(temp);
                DataParserGleonServiceXML.XmlSource test =
                        ((DataParserGleonServiceXML.XmlSource)
                        sourceList.getSelectedValue());
                ((DataParserGleonServiceXML.XmlSource)
                        sourceList.getSelectedValue()).tryRetrievalCustomAsync(start, end);
                JOptionPane.showMessageDialog(null, "Download Scheduled.");
            }catch(Exception e){
                e.printStackTrace();
            }
        }
    }//GEN-LAST:event_getCustomButtonActionPerformed
    
    private void updatePanel(){
        sourceList.setListData(parser.getSources().toArray(new DataParserGleonServiceXML.XmlSource[1]));
    }
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addButton;
    private javax.swing.JButton getCustomButton;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JList sourceList;
    // End of variables declaration//GEN-END:variables
    
}
